package com.dianrui.moonfire.baidumap;

import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.route.BikingRoutePlanOption;
import com.baidu.mapapi.search.route.BikingRouteResult;
import com.baidu.mapapi.search.route.DrivingRoutePlanOption;
import com.baidu.mapapi.search.route.DrivingRouteResult;
import com.baidu.mapapi.search.route.IndoorRouteResult;
import com.baidu.mapapi.search.route.MassTransitRoutePlanOption;
import com.baidu.mapapi.search.route.MassTransitRouteResult;
import com.baidu.mapapi.search.route.OnGetRoutePlanResultListener;
import com.baidu.mapapi.search.route.PlanNode;
import com.baidu.mapapi.search.route.RoutePlanSearch;
import com.baidu.mapapi.search.route.TransitRoutePlanOption;
import com.baidu.mapapi.search.route.TransitRouteResult;
import com.baidu.mapapi.search.route.WalkingRoutePlanOption;
import com.baidu.mapapi.search.route.WalkingRouteResult;

/**
 * Created by leilei on 2018/4/21.
 */

public class RoutePlanning {
    private RoutePlanSearch routePlanSearch;
    private PlanNode start;
    PlanNode end;
    private OnWalkingCallBack onWalkingCallBack;
    private OnBikingCallBack onBikingCallBack;
    private OnTransitCallBack onTransitCallBack;
    private OnMassTransitCallBack onMassTransitCallBack;
    private OnIndoorCallBack onIndoorCallBack;
    private OnDrivingCallBack onDrivingCallBack;


    public void setOnWalkingCallBack(OnWalkingCallBack onWalkingCallBack) {
        this.onWalkingCallBack = onWalkingCallBack;
    }

    public void setOnBikingCallBack(OnBikingCallBack onBikingCallBack) {
        this.onBikingCallBack = onBikingCallBack;
    }

    public void setOnTransitCallBack(OnTransitCallBack onTransitCallBack) {
        this.onTransitCallBack = onTransitCallBack;
    }

    public void setOnMassTransitCallBack(OnMassTransitCallBack onMassTransitCallBack) {
        this.onMassTransitCallBack = onMassTransitCallBack;
    }

    public void setOnIndoorCallBack(OnIndoorCallBack onIndoorCallBack) {
        this.onIndoorCallBack = onIndoorCallBack;
    }

    public void setOnDrivingCallBack(OnDrivingCallBack onDrivingCallBack) {
        this.onDrivingCallBack = onDrivingCallBack;
    }

    private OnGetRoutePlanResultListener listener = new OnGetRoutePlanResultListener() {
        @Override
        public void onGetWalkingRouteResult(WalkingRouteResult walkingRouteResult) {
            //获取步行线路规划结果
            if (onWalkingCallBack != null) {
                onWalkingCallBack.onWalkingCallBack(walkingRouteResult);
            }
        }

        @Override
        public void onGetTransitRouteResult(TransitRouteResult transitRouteResult) {
            //获取出租线路规划结果
            if (onTransitCallBack != null) {
                onTransitCallBack.onTransitCallBack(transitRouteResult);
            }
        }

        @Override
        public void onGetMassTransitRouteResult(MassTransitRouteResult massTransitRouteResult) {
            //获取跨城综合公共交通线路规划结果
            if (onMassTransitCallBack != null) {
                onMassTransitCallBack.onMassTransitCallBack(massTransitRouteResult);
            }
        }

        @Override
        public void onGetDrivingRouteResult(DrivingRouteResult drivingRouteResult) {
            //获取驾车线路规划结果
            if (onDrivingCallBack != null) {
                onDrivingCallBack.onDrivingCallBack(drivingRouteResult);
            }
        }

        @Override
        public void onGetIndoorRouteResult(IndoorRouteResult indoorRouteResult) {
            //获取室内线路规划结果
            if (onIndoorCallBack != null) {
                onIndoorCallBack.onIndoorCallBack(indoorRouteResult);
            }
        }

        @Override
        public void onGetBikingRouteResult(BikingRouteResult result) {
            //获取骑行线路规划结果
            if (onBikingCallBack != null) {
                onBikingCallBack.onBikingCallBack(result);
            }
        }
    };

    /**
     * 初始化
     */
    public void init() {
        routePlanSearch = RoutePlanSearch.newInstance();
        routePlanSearch.setOnGetRoutePlanResultListener(listener);

    }

    /**
     * 设置起终点坐标
     *
     * @param start_city_name 起点城市名
     * @param start_address   起点地址
     * @param end_city_name   终点城市名
     * @param end_address     终点地址
     */
    public void setByCityNameAndPlaceName(String start_city_name, String start_address, String end_city_name, String end_address) {
        start = PlanNode.withCityNameAndPlaceName(start_city_name, start_address);
        end = PlanNode.withCityNameAndPlaceName(end_city_name, end_address);
    }


    /**
     * 设置起终点坐标
     *
     * @param start_city_code 起点城市代码
     * @param start_address   起点地址
     * @param end_city_code   终点城市代码
     * @param end_address     终点地址
     */
    public void setByCityCodeAndPlaceName(int start_city_code, String start_address, int end_city_code, String end_address) {
        start = PlanNode.withCityCodeAndPlaceName(start_city_code, start_address);
        end = PlanNode.withCityCodeAndPlaceName(end_city_code, end_address);
    }

    /**
     * 设置起终点坐标
     *
     * @param start_ 起点坐标
     * @param end_   终点坐标
     */
    public void setByLocation(LatLng start_, LatLng end_) {
        start = PlanNode.withLocation(start_);
        end = PlanNode.withLocation(end_);
    }

    /**
     * 开始搜索线路
     *
     * @param what        1步行模式 2骑行模式 3室内模式 4驾车模式 5城市公交 6Taxi
     * @param ridingType  骑行模式才传，其余模式传-1即可 0：普通骑行模式，1：电动车模式。
     */
    public void startSearch( int what,int ridingType) {
        switch (what){
            case 1:
                routePlanSearch.walkingSearch((new WalkingRoutePlanOption())
                        .from(start)
                        .to(end));
                break;
            case 2:
                routePlanSearch.bikingSearch((new BikingRoutePlanOption().ridingType(ridingType))
                        .from(start)
                        .to(end));
                break;
            case 3:
//                routePlanSearch.walkingIndoorSearch((new IndoorRoutePlanOption())
//                        .from(start)//IndoorPlanNode
//                        .to(end));//IndoorPlanNode
                break;
            case 4:
                routePlanSearch.drivingSearch((new DrivingRoutePlanOption())
                        .from(start)
                        .to(end));
                break;
            case 5:
                routePlanSearch.masstransitSearch((new MassTransitRoutePlanOption())
                        .from(start)
                        .to(end));
                break;
            case 6:
                routePlanSearch.transitSearch((new TransitRoutePlanOption())
                        .from(start)
                        .to(end));
                break;
        }

    }


    public void release() {
        if (routePlanSearch != null) {
            routePlanSearch.destroy();
        }
    }




    public interface OnWalkingCallBack {
        void onWalkingCallBack(WalkingRouteResult result);
    }

    public interface OnBikingCallBack {
        void onBikingCallBack(BikingRouteResult result);
    }

    public interface OnTransitCallBack {
        void onTransitCallBack(TransitRouteResult result);
    }

    public interface OnMassTransitCallBack {
        void onMassTransitCallBack(MassTransitRouteResult result);
    }

    public interface OnIndoorCallBack {
        void onIndoorCallBack(IndoorRouteResult result);
    }

    public interface OnDrivingCallBack {
        void onDrivingCallBack(DrivingRouteResult result);
    }


}
